# Contributor: Chocobo1 <https://github.com/Chocobo1>

_realname=qbittorrent
pkgbase=mingw-w64-${_realname}
pkgname=${MINGW_PACKAGE_PREFIX}-${_realname}
pkgver=4.5.5
pkgrel=1
pkgdesc="An advanced BitTorrent client programmed in C++, based on Qt toolkit and libtorrent-rasterbar (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url="https://qbittorrent.org/"
license=('spdx:GPL-2.0-or-later')
depends=("${MINGW_PACKAGE_PREFIX}-boost"
         "${MINGW_PACKAGE_PREFIX}-openssl"
         $([[ ${CARCH} != i686 ]] && echo \
           "${MINGW_PACKAGE_PREFIX}-qt6-base" \
           "${MINGW_PACKAGE_PREFIX}-qt6-svg" || echo \
           "${MINGW_PACKAGE_PREFIX}-qt5-base" \
           "${MINGW_PACKAGE_PREFIX}-qt5-svg" \
           "${MINGW_PACKAGE_PREFIX}-qt5-winextras")
         "${MINGW_PACKAGE_PREFIX}-libtorrent-rasterbar"
         "${MINGW_PACKAGE_PREFIX}-zlib")
optdepends=("${MINGW_PACKAGE_PREFIX}-python: needed for torrent search tab")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             $([[ ${CARCH} != i686 ]] && echo \
               "${MINGW_PACKAGE_PREFIX}-qt6-tools" || echo \
               "${MINGW_PACKAGE_PREFIX}-qt5-tools"))
source=("https://downloads.sourceforge.net/sourceforge/qbittorrent/${_realname}-${pkgver}.tar.xz"{,.asc}
        001-PROCESS_CLASS_INFORMATION-require-win8.patch
        002-winconf-prepare-env-for-mingw.patch
        003-no-file-offset-bits-64.patch
        004-unixy-install.patch
        005-no-invalid-redef.patch)
sha256sums=('8e84a5ba63c7203cfb2e5bb23e27d000120fb8e9dfb6e9f9a1064dce1b9294e9'
            'SKIP'
            'a89b2830259203734e30c02f40104bf2898ced576543a5b0edc559fe1884e0b4'
            'd795153d102474e405bef3ca35fce5828432b385813efb0a3c6562999c8e1339'
            'd1ff16cd4cd955532bc0db98c8f3714003e0dc1daaab8f2eee8c51c687ca413b'
            '3e63846fff96696c7883540bac2a3e0ad51f7bd0ca6290245885d981b50e0045'
            '9fc2825249fd4146afa0da9ab52cd882f5dad073c1d4796183775cf712fdcb07')
validpgpkeys=('D8F3DA77AAC6741053599C136E4A2D025B7CC9A2') # sledgehammer_999 <hammered999@gmail.com>

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  patch -p0 -i ${srcdir}/001-PROCESS_CLASS_INFORMATION-require-win8.patch

  patch -Np1 -i "${srcdir}/002-winconf-prepare-env-for-mingw.patch"

  # This defines "stat" which clashes with libtorrent headers and breaks the build.
  # From what I see off_t isn't used in the code, so this shouldn't change anything
  patch -Np1 -i "${srcdir}/003-no-file-offset-bits-64.patch"

  # mainly to get man pages
  patch -Np1 -i "${srcdir}/004-unixy-install.patch"

  if [[ ${CARCH} != i686 ]]; then
    # somehow qt6 defines _WIN32_WINNT=0x0A00, and qbittorrent also
    # defining it to a differnt value errors out. make it match.
    patch -Np1 -i "${srcdir}/005-no-invalid-redef.patch"
  fi
}

build() {
  cp -rf ${_realname}-${pkgver} build-${MSYSTEM} && cd ${srcdir}/build-${MSYSTEM}

  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  if [[ ${CARCH} != i686 ]]; then
    extra_config+=("-DQT6=ON")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    "${MINGW_PREFIX}"/bin/cmake.exe \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      -DSTACKTRACE=OFF \
      "${extra_config[@]}" \
      ../${_realname}-${pkgver}

  "${MINGW_PREFIX}"/bin/cmake.exe --build .
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"

  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install .

  install -Dm644 "COPYING" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"
}
